<?php

/**
 * 公告板管理
 * @author administrator
 *
 */
class newsManage extends CControllerManageBase {

    public function show(){
    	$model = new newsModel(); 
		$pageIndex = $this->getRequestData("page","GET",1);
		$where = "del_flag=0";
		$title=$this->getRequestData("txt_title","get","");
		if($_POST)
		{
			$this->updateSortNum($model);
		}
		
		$pageSize = 10;
		$c = 0;

		$baseUrl = "";
		if($title)
		{
			$pageIndex = 1;
			$where .= " and title like '%".$title."%'";
			$baseUrl = "?t=".urlencode($title);
		}

		$list = $model->selectPage($pageIndex,$pageSize,"news_id,title,pub_time,sort_num",$where,"sort_num desc,pub_time desc","",$c);

		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$baseUrl);
		
		$this->setMenu("公告板管理");
		$this->render("news/list",array("list"=>$list,"pageStr"=>$pageStr,"title"=>$title));
    }
    
    private $_arrRule = array(
            'txt_title'=>array(
                'formValidator'=>array('tipid'=>'txt_title_tip','onshow'=>'请输入标题','onfocus'=>'请输入标题','oncorrect'=>'输入正确'),
                'inputValidator'=>array('min'=>1,'onerrormin'=>'标题不能为空')
            ),
            'txt_sort'=>array(
                'formValidator'=>array('tipid'=>'txt_sort_tip','onshow'=>'默认按排数字从大到小排序，不填则默认取最大值+10','onfocus'=>'请输入排序数字','oncorrect'=>'正确'),
                "regexValidator" => array(
								"regexp" => "^[0-9]{1,9}$",
								"onerror" => "排序数字不正确"
						)
            )
        );
    
    public function add(){
        $strError = '';
        $model = new newsModel();
        
        $this->formValidator->init(array('formID'=>'newsForm'));
        $this->formValidator->setRules($this->_arrRule);
        if($_POST){
             $arr['title'] = $this->getRequestData('txt_title');
             $arr['sort_num'] = $this->getRequestData('txt_sort');
             $arr['pub_time'] = $this->getRequestData('txt_pub_time');
             if(!$arr['pub_time'])
             {
             	$arr['pub_time'] = date("Y-m-d H:i:s");
             }
             $arr["create_time"] = date("Y-m-d H:i:s");
             $arr["create_user"] = $this->_userID;
             $content = $this->getRequestData('txt_content',"POST","",false);
             
             $arr['content'] = $content; 
             $arrError = $this->formValidator->run();
            if ($arrError) {
                $strError = "<div><ul>";
                foreach ($arrError as $item) {
                    $strError .= "<li>" . $item . "</li>";
                }
                $strError .= "</ul></div>";
            }else{
                $iRtn = $model->insert($arr);
                if($iRtn !== false){
                    $this->sysMsg("新增成功",0,array(array('text'=>'返回列表','href'=>$this->_manageDomain.'newsManage/show'),array("text"=>"继续新增","href"=>$this->_manageDomain."newsManage/add")));
                }else{
                    $this->sysMsg("新增失败",1);
                }
            }
        }
        $this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js","/kindeditor/kindeditor-min.js", "/kindeditor/lang/zh_CN.js","/My97DatePicker/WdatePicker.js"));
        $this->setJsBlock("var editor;
        KindEditor.ready(function(K) {
                editor = K.create('#txt_content');
        });");

        
        $maxSort = $model->getMaxSort();
        $this->setStyleFile("/css/validator.css");
		
        $this->setMenu(array(
            array('text'=>'公告板管理','href'=>$this->_manageDomain.'newsManage/show'),
            array('text'=>"新增")
        ));
        $this->render('news/add',array(
            'error'=>$strError,"maxSort"=>$maxSort,"now"=>date("Y-m-d")
        ));
    }
    
    /*private function updateLongData($id,$content)
    {
    	$sql = "SELECT
		CONTENT
		FROM
		t_news
		WHERE
		news_id = {$id}
		FOR update
		";
		$conn = OCILogon("scott", "123456");
		$stmt = oci_parse($conn, $sql);
		
		// Execute the statement using OCI_DEFAULT (begin a transaction)
		oci_execute($stmt, OCI_DEFAULT)
		or die ("Unable to execute query\n");
		
		// Fetch the SELECTed row
		if ( FALSE === ($row = oci_fetch_assoc($stmt) ) ) {
		oci_rollback($conn);
		die ("Unable to fetch row\n");
		}
		
		//var_dump($row['CONTENT']->load());
		
		var_dump($row);exit;
		// Discard the existing LOB contents
		if ( !$row['CONTENT']->truncate() ) {
		oci_rollback($conn);
		die ("Failed to_ truncate LOB\n");
		}
		
		// Now save a value to the LOB
		if ( !$row['CONTENT']->save($content)) {
		
		// On error, rollback the transaction
			oci_rollback($conn);
			echo "error";
		} else {
			echo "success";
		// On success, commit the transaction
			oci_commit($conn);
		}

		// Free resources
		oci_free_statement($stmt);
		$row['CONTENT']->free();
    	//exit;
    }*/
    
	public function edit(){
		
		$id = $this->getRequestData("id","GET");
        $strError = '';
        $model = new newsModel();
        
        $this->formValidator->init(array('formID'=>'newsForm'));
        $this->formValidator->setRules($this->_arrRule);
        if($_POST){
        	$arr["news_id"] = $id;
			$arr['title'] = $this->getRequestData('txt_title');
			$arr['sort_num'] = $this->getRequestData('txt_sort');
			$arr['pub_time'] = $this->getRequestData('txt_pub_time');
        	if(!$arr['pub_time'])
             {
             	$arr['pub_time'] = date("Y-m-d H:i:s");
             }
			$arr['content'] = $this->getRequestData('txt_content',"POST","",false);
			$arrError = $this->formValidator->run();
            if ($arrError) {
                $strError = "<div><ul>";
                foreach ($arrError as $item) {
                    $strError .= "<li>" . $item . "</li>";
                }
                $strError .= "</ul></div>";
            }else{
                $model->updateByPkId($arr);
                
                header("location:".$this->_manageDomain."newsManage/show");
                exit;
            }
        }
        else
        {
        	$modelInfo = $model->selectById($id,"del_flag=0","news_id,title,pub_time,sort_num,content");
			if(!$modelInfo) {
				$this->sysMsg("记录不存在",1);
			}
			$modelInfo['content'] = stripslashes($modelInfo['content']);
        }

        $this->setStyleFile("/css/validator.css");
        $this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js","/kindeditor/kindeditor-min.js", "/kindeditor/lang/zh_CN.js","/My97DatePicker/WdatePicker.js"));
        $this->setJsBlock("var editor;
        KindEditor.ready(function(K) {
                editor = K.create('#txt_content');
        });");
        
        $this->setMenu(array(
            array('text'=>'新闻管理','href'=>$this->_manageDomain.'newsManage/show'),
            array('text'=>"修改")
        ));
        $this->render('news/edit',array(
        	"modelInfo"=>$modelInfo,
            'error'=>$strError
        ));
    }
    
	public function del()
	{
		$id = $this->getRequestData("id","GET");
		if ($id)
        {
            $model = new newsModel();
            
			$model->deleteByPkId($id);
            header("location:".$this->_manageDomain."newsManage/show");
            exit;
        }
	}
	
	public function batchDel()
	{
		$ids = $this->getRequestData("ids","GET");
		
		$idsList = explode(",",$ids);
		 
		$model = new newsModel(); 
		foreach($idsList as $id)
		{
            $model->deleteByPkId($id);
		}
		header("location:".$this->_manageDomain."newsManage/show");
		exit;
	}
}

?>
